# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:gongbao_sx3_0.py
@Time:2024/9/3 14:42

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""
import requests
from bs4 import BeautifulSoup
from docx import Document
import os
import re

# 定义URL
# url = 'http://tjj.shanxi.gov.cn/tjsj/tjgb/ndtjgb/202403/t20240320_9522751.shtml'

def url2docx(url):
    # 发送请求
    response = requests.get(url)

    # 检查请求是否成功
    if response.status_code == 200:
        response.encoding = "utf-8"  # 设置正确的编码
        # 解析HTML
        soup = BeautifulSoup(response.text, 'html.parser')

        # 找到包含公报内容的 div 元素
        # 注意：这里使用了一个假设的 class 名称，实际使用时需要根据页面结构进行调整
        pattern = re.compile(r'view TRS_UEDITOR trs_paper_default trs_word|TRS_Editor|view TRS_UEDITOR trs_paper_default trs_external')
        # reports_div = soup.find('div', class_='view TRS_UEDITOR trs_paper_default trs_word')
        # reports_div = soup.find('div', class_='TRS_Editor')#view TRS_UEDITOR trs_paper_default trs_external
        reports_div = soup.find('div',class_=pattern)
        reports_title = soup.find('div', class_='detail-article-title clearfix')
        reports_title = reports_title.text.strip().replace('\n','')
        if reports_title:
            print(reports_title)

        if reports_div:
            # 创建一个新的 Word 文档
            doc = Document()
            doc.add_paragraph(reports_title)  # 修正拼写错误

            # 在找到的 div 元素内部查找所有的 p 标签
            report_p = reports_div.find_all('p')

            # 添加所有找到的公报内容到 Word 文档中，忽略空行
            for report in report_p:
                text = report.text.strip()  # 去除前后空白字符
                if text:  # 只添加非空的文本
                    doc.add_paragraph(text)

            # 保存文档
            output_dir = './output_gongbao_sx/'
            if not os.path.exists(output_dir):
                os.makedirs(output_dir)
            doc.save(os.path.join(output_dir, reports_title + '.docx'))
            print("Document saved successfully.")
        else:
            print("No <div> element found with the specified class.")
    else:
        print("Failed to retrieve the webpage:", response.status_code)

# 定义URL
url = 'http://tjj.shanxi.gov.cn/tjsj/tjgb/ndtjgb/202403/t20240320_9522751.shtml'
url2docx(url)